package com.dianrui.fmi.view.wheelviews;

import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.dianrui.fmi.R;
import com.dianrui.fmi.view.wheelviews.wheelview.OnItemSelectedListener;
import com.dianrui.fmi.view.wheelviews.wheelview.WheelView;

import java.util.ArrayList;
import java.util.List;

/**
 * 描    述：
 * 创建日期：2017/7/20 14:26
 * 作    者：Chengfu
 * 邮    箱：
 * 备    注：
 */
public class SexPicker extends Dialog implements View.OnClickListener {

    private static int MIN_YEAR = 1900;
    private static int MAX_YEAR = 2200;
    private View view;
    public WheelView mYearWheelView;
    private WheelView mMonthWheelView;
    private WheelView mDayWheelView;
    private TextView mTvConfirm;
    private TextView mTvCancel;
    private OnSexCListener mOnSexCListener;

    private List<String> years = new ArrayList<>();
    private List<String> months = new ArrayList<>();
    private List<String> days = new ArrayList<>();

    private int yearPos;
    private int monthPos;
    private int dayPos;


    public void clear() {
        view = null;
        mYearWheelView = null;
        mMonthWheelView = null;
        mDayWheelView = null;
        mTvConfirm = null;
        mTvCancel = null;
        mOnSexCListener = null;
        if (years != null) {
            years.clear();
        }
        years = null;

        if (months != null) {
            months.clear();
        }
        months = null;

        if (days != null) {
            days.clear();
        }
        days = null;
    }


    public SexPicker(Context context) {
        super(context, R.style.transparentWindowStyle);

        view = View.inflate(context, R.layout.layout_sex_picker, null);

        initView();
        initData(context);
        setListener();

        this.setContentView(view);

        this.setCanceledOnTouchOutside(true);

        //从底部弹出
        Window window = this.getWindow();
        window.setGravity(Gravity.BOTTOM);  //此处可以设置dialog显示的位置
        window.setWindowAnimations(R.style.windowAnimationStyle);  //添加动画

        WindowManager.LayoutParams params = window.getAttributes();
        params.width = WindowManager.LayoutParams.MATCH_PARENT;
        params.height = WindowManager.LayoutParams.WRAP_CONTENT;
        window.setAttributes(params);
    }


    /**
     * 回调接口
     */
    public interface OnSexCListener {
        void onSexSelected(String sex);
    }

    public void setDateListener(OnSexCListener mOnSexCListener) {
        this.mOnSexCListener = mOnSexCListener;
    }

    private void initView() {
        mYearWheelView = (WheelView) view.findViewById(R.id.wv_province);

        mTvConfirm = (TextView) view.findViewById(R.id.tv_confirm);
        mTvCancel = (TextView) view.findViewById(R.id.tv_cancel);

        // 设置可见条目数量
        mYearWheelView.setVisibleItemCount(9);


//
        mYearWheelView.isCenterLabel(true);

    }

    private void setListener() {
        //年份*****************************
        mYearWheelView.setOnItemSelectedListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(int index) {
                yearPos = index;

            }
        });


        mTvConfirm.setOnClickListener(this);
        mTvCancel.setOnClickListener(this);
    }

    private void initData(Context context) {

        years.add(context.getResources().getString(R.string.man));
        years.add(context.getResources().getString(R.string.female));
        mYearWheelView.setItems(years);
        mYearWheelView.setCurrentItem(yearPos);



    }


    @Override
    public void onClick(View v) {
        int i = v.getId();
        if (i == R.id.tv_confirm) {
            if (mOnSexCListener != null) {
                mOnSexCListener.onSexSelected(years.get(mYearWheelView.getCurrentItem()));
            }
        }
        cancel();
    }
}
